package com.zjitc.controll;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.zjitc.po.CountryPo;
import com.zjitc.service.CountryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/26 15:09
 * @description:
 */
@Controller
public class CountryControll {

  @Autowired
  public CountryService countryService;

  @RequestMapping("/findAll")
  public ModelAndView findCountry(){
    List<CountryPo> list = countryService.findCountry();
    ModelAndView mv = new ModelAndView();
    System.out.println(list);
    mv.addObject("countryList",list);
    mv.setViewName("/views/display.jsp");
    return mv;
  }

  @RequestMapping("/findByPage")
  public ModelAndView findCountries(
      @RequestParam(name = "start",required = false,defaultValue = "1")int start,
      @RequestParam(name = "end",required = false,defaultValue = "10")int end){
    List<CountryPo> countries = countryService.findContries(start,end);
    StringBuilder builder=new StringBuilder();
    for (CountryPo country:countries){
      builder.append(country.toString());
      builder.append("\n");
    }
    System.out.println(builder);
    System.out.println((Page) countries);
    PageInfo info=new PageInfo(countries);
    System.out.println(info);
    ModelAndView mv = new ModelAndView();
    mv.addObject("countryList",countries);
    mv.setViewName("/views/display.jsp");
    return mv;
  }
}
